<?php

namespace App\Transformers\Encyclopedia;

use App\Transformers\Traits\WithTransformerAuthor;
use App\Transformers\Traits\WithTransformerMedia;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\TransformerAbstract;

class ArticleTransformer extends TransformerAbstract
{
    use WithTransformerAuthor, WithTransformerMedia;

    protected $availableIncludes = ['author', 'category', 'content', 'media'];

    public function transform(Model $model)
    {
        return $model->attributesToArray();
    }

    public function includeCategory(Model $model)
    {
        $category = $model->category;
        if (!$category) {
            return $this->null();
        }
        return $this->item($category, new CategoryTransformer());
    }

    public function includeContent(Model $model)
    {
        $content = $model->content;
        if (!$content) {
            return $this->null();
        }
        return $this->item($content, new ContentTransformer());
    }
}